package com.power.sifa.correction.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.correction.model.Crw_rule_zoneout;
import com.power.sifa.correction.model.pageInfo.Crw_rule_zoneoutPageInfo;
import com.power.sifa.correction.api.ICrw_rule_zoneoutFeign;
import com.power.sifa.correction.service.Crw_rule_zoneoutService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   禁出圈设置控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Crw_rule_zoneoutController",tags={"禁出圈设置"})
public class Crw_rule_zoneoutController implements ICrw_rule_zoneoutFeign{

    @Resource
    private Crw_rule_zoneoutService crw_rule_zoneoutService;

    
    @Override
    @ApiOperation("查询禁出圈设置分页列表")
    @SaCheckPermission("crw_rule_zoneout_list")
    public ResponseDTO list(Crw_rule_zoneoutPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Crw_rule_zoneout> page = this.crw_rule_zoneoutService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Crw_rule_zoneout crw_rule_zoneout : page) {
                crw_rule_zoneout.geneText();
                list.add(crw_rule_zoneout);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看禁出圈设置")
    @SaCheckPermission("crw_rule_zoneout_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Crw_rule_zoneout crw_rule_zoneout  = crw_rule_zoneoutService.getById(id);
        if (crw_rule_zoneout != null) {
            crw_rule_zoneout.geneText();
        }
        return ResponseDTO.succData(crw_rule_zoneout);
    }

    @Override
    @ApiOperation("删除禁出圈设置")
    @SaCheckPermission("crw_rule_zoneout_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        crw_rule_zoneoutService.removeRule(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改禁出圈设置")
    @SaCheckPermission("crw_rule_zoneout_save")
    public ResponseDTO save(Crw_rule_zoneout crw_rule_zoneout) {
        Assert.notNull(crw_rule_zoneout, "crw_rule_zoneout不能为空");
        crw_rule_zoneoutService.saveOrUpdate(crw_rule_zoneout);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="批量删除禁出圈设置")
    @SaCheckPermission("crw_rule_zoneout_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        crw_rule_zoneoutService.removeByIds(list);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("crw_rule_zoneout_list")
    public void output(HttpServletResponse response, Crw_rule_zoneoutPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "禁出圈设置";
        String[] heads = {"a序号","规则名称","人员姓名s","状态"};
        String[] fields = {"id","name","pernames","status"};
        Page<Crw_rule_zoneout> page = this.crw_rule_zoneoutService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }
            
    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "禁出圈设置";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"a序号","规则名称","人员姓名s","状态"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }
    
	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","name","pernames","status"};
        List<Crw_rule_zoneout> list = (List<Crw_rule_zoneout>)ExcelUtil.Import(fields, file, Crw_rule_zoneout.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.crw_rule_zoneoutService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    @Override
    public ResponseDTO saveRule(Crw_rule_zoneout crw_rule_zoneout, String personIds, String groupIds, String geopointIds) {
        Assert.notNull(crw_rule_zoneout, "crw_rule_zoneout不能为空");
        Assert.notNull(personIds, "personIds不能为空");
        Assert.notNull(groupIds, "geopointIds不能为空");
        Assert.notNull(geopointIds, "geopointIds不能为空");
        List<String> personIdList = StringUtils.hasText(personIds) ? Arrays.asList(personIds.split(",")) : Collections.emptyList();
        List<String> groupIdList = StringUtils.hasText(groupIds) ? Arrays.asList(groupIds.split(",")) : Collections.emptyList();
        List<String> geopointIdList = StringUtils.hasText(geopointIds) ? Arrays.asList(geopointIds.split(",")) : Collections.emptyList();
        this.crw_rule_zoneoutService.saveRule(crw_rule_zoneout,personIdList,groupIdList,geopointIdList);
        return ResponseDTO.succ();
    }

    @Override
    public ResponseDTO<Boolean> existsRuleName(String name) {
        Assert.notNull(name,"规则名称无值");

        return ResponseDTO.succData(crw_rule_zoneoutService.existsRuleName(name));
    }

}
